{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "\n",
    "import sys\n",
    "try:\n",
    "    reload(sys)\n",
    "    sys.setdefaultencoding('gbk')\n",
    "except:\n",
    "    pass\n",
    "\n",
    "import codecs\n",
    "from textrank4zh import TextRank4Keyword, TextRank4Sentence\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "关键词：\n",
      "\n",
      "关键短语：\n",
      "\n",
      "摘要：\n"
     ]
    }
   ],
   "source": [
    "text = codecs.open('data/1.txt', 'r', 'gbk').read()\n",
    "tr4w = TextRank4Keyword()\n",
    "\n",
    "tr4w.analyze(text=text, lower=True, window=2)  # py2中text必须是utf8编码的str或者unicode对象，py3中必须是utf8编码的bytes或者str对象\n",
    "\n",
    "print( '关键词：' )\n",
    "for item in tr4w.get_keywords(20, word_min_len=1):\n",
    "    print(item.word, item.weight)\n",
    "\n",
    "print()\n",
    "print( '关键短语：' )\n",
    "for phrase in tr4w.get_keyphrases(keywords_num=20, min_occur_num= 2):\n",
    "    print(phrase)\n",
    "\n",
    "tr4s = TextRank4Sentence()\n",
    "tr4s.analyze(text=text, lower=True, source = 'all_filters')\n",
    "\n",
    "print()\n",
    "print( '摘要：' )\n",
    "for item in tr4s.get_key_sentences(num=3):\n",
    "    print(item.index, item.weight, item.sentence)  # index是语句在文本中位置，weight是权重"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
